package org.mags.exception.common;

import org.apache.commons.validator.GenericValidator;

public abstract class AbstractExceptionHandler implements ExceptionHandler {
	private int order;

	protected String extractExtraDetail(Object exception) {
		if(exception instanceof ExtraDetailException) {
			String extraDetail = ((ExtraDetailException)exception).getExtraDetail();
			if(extraDetail!=null)
				return extraDetail;
		}
		return "";
	}
	protected String createExtraData(Throwable exception) {
		String extraData = extractExtraData(exception);
		if(extraData==null) extraData = "";
		if(!GenericValidator.isBlankOrNull(extraData))
			extraData = " ["+extraData+"]";
		return extraData;
	}
	protected String getStackTrace(Throwable exception) {
		return ExceptionUtils.getStackTrace(exception);
	}
	@Override
	public void setOrder(int order) {
		this.order = order;
	}
	@Override
	public int getOrder() {
		return order;
	}
	protected String extractExtraData(Object exception) {
		if(exception instanceof ExtraDataException) {
			String dataToShow = ((ExtraDataException)exception).getDataToShow();
			if(dataToShow!=null)
				return dataToShow;
		}
		return "";
	}
	protected String appendExtraData(Object exception, String message) {
		String dataToShow = extractExtraData(exception);
		if(dataToShow!=null&&!"".equals(dataToShow)) {
			if(message==null)
				message = dataToShow;
			else
				message = message + " ["+dataToShow+"]";
		}
		return message;
	}
}
